package com.example.random_your_hero.service.impl;

import com.example.random_your_hero.model.Hero;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import com.example.random_your_hero.service.IHeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HeroServiceImpl implements IHeroService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Hero> getList() {
        String sql = "SELECT * FROM hero;";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        List<Hero> hList = new ArrayList();
        list.forEach(m -> {
            Hero hero = new Hero();
            hero.setId((Integer) m.get("id"));
            hero.setName((String) m.get("name"));
            hero.setBackgroundStory((String) m.get("background_story"));
            hero.setCourtesyTitle((String) m.get("courtesy_title"));
            hero.setPictureFile((String) m.get("picture_file"));
            hero.setInfo((String) m.get("info"));
            hList.add(hero);
        });

        return hList;
    }

    @Override
    public List<Hero> getRandomList(String number) {
        String sql = "SELECT * FROM hero ORDER BY RANDOM() LIMIT "+number+" ;";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        List<Hero> hList = new ArrayList();
        list.forEach(m -> {
            Hero hero = new Hero();
            hero.setId((Integer) m.get("id"));
            hero.setName((String) m.get("name"));
            hero.setBackgroundStory((String) m.get("background_story"));
            hero.setCourtesyTitle((String) m.get("courtesy_title"));
            hero.setPictureFile((String) m.get("picture_file"));
            hero.setInfo((String) m.get("info"));
            hList.add(hero);
        });

        return hList;
    }

    @Override
    public String add(Hero hero) {
        String sql =  String.format("INSERT INTO hero (name,courtesy_title,background_story,picture_file,info) VALUES('%s','%s','%s','%s','%s');",hero.getName(),hero.getCourtesyTitle(),hero.getBackgroundStory(),hero.getPictureFile(),hero.getInfo());
        int res = jdbcTemplate.update(sql);
        return null;
    }

    @Override
    public String updateById(Hero hero) {
        String sql = String.format("UPDATE hero SET name = '%s',courtesy_title='%s',background_story='%s',picture_file='%s',info = '%s' WHERE id = '%s';",hero.getName(),hero.getCourtesyTitle(),hero.getBackgroundStory(),hero.getPictureFile(),hero.getInfo(),hero.getId());
        int res = jdbcTemplate.update(sql);
        return "ok";
    }

    @Override
    public Map<String, Object> searchById(String id) {
        String sql = String.format("SELECT * FROM hero WHERE id = %s",id);
        Map<String, Object> map = jdbcTemplate.queryForObject(sql,new ColumnMapRowMapper());
        return map;
    }

    @Override
    public String deleteById(HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("id");
        String sql = String.format("DELETE FROM  hero WHERE id = '%s';",id);
        int res = jdbcTemplate.update(sql);
        return "ok";
    }
}
